package org.apak.frontend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.control.ErrorList;
import org.apak.control.ServiceActionForm;
import org.apak.control.ServicePartForm;
import org.apak.control.Validator;
import org.apak.db.RepairServiceDAO;
import org.apak.log.FileLogger;
import org.apak.model.Device;
import org.apak.model.LoggedSubject;
import org.apak.model.ServicePart;
import org.apak.model.ServiceType;

/**
 * Servlet implementation class ServicePartServlet
 */
public class ServicePartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairServiceDAO dao = new RepairServiceDAO();
	private Validator validator = new Validator();
	private String serviceOrderID;
	private String id;
	private HttpSession session;
	LoggedSubject loggedSubject;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null || loggedSubject.isCustomer()) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			serviceOrderID = request.getParameter("serviceOrder");
			id = request.getParameter("id");
			if(id != null) {
				ServicePart servicePart = dao.getServicePart(id);
				request.setAttribute("servicePart", servicePart);
				serviceOrderID = Integer.toString(servicePart.getService_order_fk());
			}
			if(dao.getServiceOrder(serviceOrderID) == null) {
				//TODO: PRINT ERROR PAGE!!!!
			}
			if(serviceOrderID != null) {
				Device[] deviceArray = dao.getDeviceList(serviceOrderID);
				request.setAttribute("deviceArray", deviceArray);
				request.setAttribute("serviceOrderID", serviceOrderID);
			}
			FileLogger.logMessage("Forwarded to servicePart.jsp by ServicePartServlet.doGet().");
			RequestDispatcher dispatcher = request.getRequestDispatcher("servicePart.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Device[] deviceArray = dao.getDeviceList(serviceOrderID);
		request.setAttribute("deviceArray", deviceArray);
		request.setAttribute("serviceOrderID", serviceOrderID);
		String actionButton = request.getParameter("actionButton");
		ServicePartForm servicePartForm = new ServicePartForm(
				request.getParameter("device"), request.getParameter("part_name"), serviceOrderID,
				request.getParameter("serial_no"), request.getParameter("part_count"), request.getParameter("part_price"));
		if(actionButton != null)
			if(actionButton.equals("Save")) {
				ErrorList errors = validator.findErrors(servicePartForm);
				if(errors.isErrorFound()) {
					request.setAttribute("errors", errors);
					FileLogger.logMessage("Forwarded to servicePart.jsp by ServicePartServlet.doPost().");
					RequestDispatcher dispatcher = request.getRequestDispatcher("servicePart.jsp");
					dispatcher.forward(request, response);
				} else {
					if(id != null) {
						if(dao.updateServicePart(servicePartForm, id) != 0)
							request.setAttribute("success", "Saved successfully!");
					}
					else if(dao.insertServicePart(servicePartForm, loggedSubject.getSubject_id()) != 0)
						request.setAttribute("success", "Saved successfully!");
					request.setAttribute("servicePartForm", servicePartForm);	
					FileLogger.logMessage("Forwarded to servicePart.jsp by ServicePartServlet.doPost().");
					RequestDispatcher dispatcher = request.getRequestDispatcher("servicePart.jsp");
					dispatcher.forward(request, response);
				}
			}
	}

}
